package com.vayne.common;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author xuerui.zhang
 * @since 2019-07-09 23:28
 */
public abstract class AbstractConsumer {

    public static final String brokerList = "kafka0:9092";
    public static final String topic = "topic-demo-two";
    public static final String groupId = "group.demo";
    static final String clientId = "consumer.client.id.demo";
    static final AtomicBoolean isRunning = new AtomicBoolean(Boolean.TRUE);

    public static Properties initConfig() {
        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        return props;
    }

    static void printMessage(ConsumerRecords<String, String> records) {
        for (ConsumerRecord<String, String> record : records) {
            System.out.println(record.offset() + ":" + record.value());
        }
    }
}
